<template>
  <div class="sys-role">
  <el-dialog
    direction="ltr"
    :show-close="false"
    :visible.sync="visible"
    :before-close="closeModal">
    <el-card class="box-card">
      <div slot="header" >
        <span>配置菜单以及权限</span>
        <el-checkbox style="float: right;margin-right: 10px" v-model="allSelected" @change="!allSelected">全选</el-checkbox>
      </div>
    <el-table
      size="small"
      :data="menuTree"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      default-expand-all
      ref="treeTable"
      :tree-props="{children: 'children', hasChildren: 'isChild'}">
<!--      <el-table-column prop="id" width="100" label="选中" >-->
<!--        <template slot-scope="scope">-->
<!--          <el-checkbox-group v-model="menuSelected" >-->
<!--            <el-checkbox style="margin-left: 1px"  :label="scope.row.id" :key="scope.row.id">{{scope.row.name}}</el-checkbox>-->
<!--          </el-checkbox-group>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column  prop="name" label="菜单名称"  width="50" align="center">
        <template slot-scope="scope">
          <el-checkbox-group v-model="menuSelected" style="display: inline">
            <el-checkbox  :label="scope.row.id" :key="scope.row.id">{{scope.row.name}}</el-checkbox>
          </el-checkbox-group>
        </template>
      </el-table-column>
      <el-table-column   label="资源"  width="180" align="center">
        <template slot-scope="scope">
          <el-checkbox-group v-model="powerSelected" >
            <el-checkbox  v-for="item in getChild(scope.row.id)" :label="item.id" :key="item.id">{{item.name}}</el-checkbox>
          </el-checkbox-group>
        </template>
      </el-table-column>
    </el-table>
    </el-card>
    <div  slot="footer" style="margin-right: 30px">
      <el-button @click="closeModal" size="small">取 消</el-button>
      <el-button v-loading="loading" type="primary" @click="submitSysRole"  size="small">确 定</el-button>
    </div>
  </el-dialog>
  </div>
</template>

<script>
import util from '@/libs/util'
import { settingRolePower, getMenuAndAuthId } from '@/api/system/role'
export default {
  name: 'sys-role',
  props: {
    // eslint-disable-next-line vue/require-valid-default-prop
    menus: { type: Array, default: [] },
    // eslint-disable-next-line vue/require-valid-default-prop
    powers: { type: Array, default: [] },
    // eslint-disable-next-line vue/require-valid-default-prop
    menuTree: { type: Array, default: [] }
  },
  data () {
    return {
      visible: false,
      roleId: '',
      loading: false,
      powerSelected: [],
      menuSelected: [],
      allSelected: false
    }
  },
  watch: {
    allSelected (v) {
      v && this.setAllSelect()
      !v && this.resetModel()
    }
  },
  created () {
  },
  methods: {
    closeModal () {
      this.resetModel()
      this.visible = false
    },
    resetModel () {
      this.powerSelected = []
      this.menuSelected = []
      this.allSelected = false
      this.loading = false
      this.roleId = ''
    },
    getChild (pid) {
      const power = []
      for (const t of this.powers) {
        if (util.eq(t.pid, pid)) {
          power.push(t)
        }
      }
      return power
    },
    submitSysRole () {
      this.loading = true
      const ids = [...this.menuSelected, ...this.powerSelected]
      this.roleId !== '' && settingRolePower({ roleId: this.roleId, ids: ids }).then(res => {
        res && util.success(this, '设置角色权限成功')
        this.closeModal()
      })
    },
    selectChange (e) {
      // console.info('选中行', e)
    },
    setAllSelect () {
      this.powerSelected = this.powers.map(e => e.id)
      this.menuSelected = this.menus.map(e => e.id)
    },
    initUserMenu () {
      getMenuAndAuthId({ roleId: this.roleId }).then(res => {
        this.powerSelected = res && res.auth && res.auth
        this.menuSelected = res && res.menus && res.menus
      })
    }
  }
}
</script>

<style scoped>
.sys-role >>> .el-dialog__header{
  padding: 0;
}
</style>
